package server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import servlet.MyCookie;
import servlet.MyResponse;
//将相应结果封装成HTTP报文
public class HTTPPackage {
	private byte[] httpHeader;
	private byte[] httpBody;//没用到
	private byte[] httpPackage;
	private MyResponse response;
	private ByteArrayOutputStream outputBuffer;
	
	public HTTPPackage(){
		
	}
	public HTTPPackage(MyResponse response){
		this.response = response;
		this.outputBuffer = response.getOutputBuffer();
		generate();
	}
	public byte[] getHttpHeader() {
		return httpHeader;
	}
	public byte[] getHttpBody() {
		return httpBody;
	}
	public byte[] getHttpPackage() {
		return httpPackage;
	}
	public void generate(){
		if(this.response!=null){
			
			int status = response.getStatus();
			StringBuffer header = new StringBuffer();
			switch (status){
				case 404: 
					header.append("HTTP/1.1 404 Not Found\n");break;
				case 200:
					header.append("HTTP/1.1 200 OK\n");break;
				default:;
			}
			
			//资源类型
			String extensionName = response.getRequest().getExtensionName();
			
			if(extensionName.equals("html")||extensionName.equals("htm")){
				response.setContentType("text/html");
			}
			else if(extensionName.equals("js")){
				response.setContentType("application/javascript");
			}
			else if(extensionName.equals("css")){
				response.setContentType("text/css");
			}
			else if(extensionName.equals("gif")){
				response.setContentType("image/gif");
			}	
			else if(extensionName.equals("jpg")){
				response.setContentType("image/jpeg");
				
			}
			else if(extensionName.equals("png")){
				response.setContentType("image/png");
			}
			else if(extensionName.equals("swf")){
				response.setContentType("application/x-shockwave-flash");
			}
			//服务器信息
			header.append("Server: MyServcer\n");
			//日期
			DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
			df.setTimeZone(TimeZone.getTimeZone("GMT")); // modify Time Zone.
			String date = df.format(new Date());
			header.append("Date: "+date+"\n");
			//内容类型
			header.append("Content-Type: "+response.getContentType());
			if(response.getCharSet()!=null){
				header.append(";charset="+response.getCharSet());
			}
			
			try {
				if(response.getMessage()!=null){
					outputBuffer.write(response.getMessage().getBytes());
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//报文体长度
			header.append("\nContent-Length: "+outputBuffer.size()+"\n");
			//设置Cookie
			if(response.getCookies().size()>0){
				header.append("Set-Cookie: ");
				for(MyCookie cookie : response.getCookies()){
					header.append(cookie.getName()+"="+cookie.getValue()+";");
				}
				header.append(" path=/");
				header.append("\n");
			}
			header.append("\n");
			//httpPackage为整个HTTP报文，包含报文头header，和报文体（从response中获取的Message体）
			httpPackage = new byte[header.toString().getBytes().length+outputBuffer.toByteArray().length];
			for (int i = 0; i < httpPackage.length; i++) {
				   if(i<header.length()){
					   httpPackage[i]=header.toString().getBytes()[i];
				   }else{
					   httpPackage[i]=outputBuffer.toByteArray()[i-header.toString().getBytes().length];
				   }
			}
			

				
		}
	}
	public void setResponse(MyResponse response) {
		this.response = response;
	}
}
